<?php

namespace app\admin\model;

use think\Model;

use think\Db;
use think\Request;

class Feedback extends Model
{
    //我的反馈 反馈列表
    public function getfeedback($res)
    {
        $page = $res['page'];
        $limit = $res['pageSize'];
        $map = array();
        if (isset($res['admin_my'])) {
            //我的反馈
            $map['create_id'] = session('userid');
        }
        $data = $this->where($map)->page($page, $limit)->order('create_at desc')->select();
        foreach ($data as $datakey => $dataval) {
            $menuids = getpostion('menu', $dataval['menu_id']);//上级模块名称id
            $menuname = $this->getmenuname($menuids, $res['lang']);
            $data[$datakey]['menuname'] = $menuname;//模块名称
            if ($dataval['param']) {
                $data[$datakey]['urlmenu'] = $dataval['url'] . '?' . $dataval['param'];
            } else {
                $data[$datakey]['urlmenu'] = $dataval['url'];
            }
            //是否可以删除按钮显示控制
            $isexit = getables('feedback_reply', ['feedback_id' => $dataval['id']]);
            if ($isexit) {
                $data[$datakey]['isexit'] = 1;
            } else {
                $data[$datakey]['isexit'] = 2;
            }
            $name=basename($dataval['imgurl']);
            //图片下载的名称
            $data[$datakey]['img']=substr($name,0,strrpos($name ,"."));
            $data[$datakey]['create_name'] = getValue('admin', ['id' => $dataval['create_id']], 'nickname');
            $data[$datakey]['create_date'] = getTimeDate($dataval['create_at'], 12);
        }
        $count = $this->where($map)->count();
        return array('count' => $count, 'data' => $data);
    }

    //上级菜单id
    protected function getmenuname($menuids, $lang)
    {
        $menuarr = array();
        foreach ($menuids as $menuidskey => $menuidsval) {
            $menumkey = 'menu_' . $menuidsval;
            array_unshift($menuarr, $menumkey);
        }
        $mapwhere['mkey'] = array('in', implode(',', $menuarr));
        return getlangname('multi_lang', $mapwhere, $lang);
    }

    //反馈的删除
    public function delfeedback($data)
    {
        $replydata = getables('feedback_reply', ['feedback_id' => $data['id']]);
        if ($replydata) {
            return array('msg' => '该反馈无法删除', 'status' => 2);
        }
        //将图片删除
        $pathurl = getValue('feedback', ['id' => $data['id']], 'imgurl');
        $usepaths = ROOT_PATH . "public" . $pathurl;
        if (is_file($usepaths)) {
            unlink($usepaths);
        }
        $res = Db::name('feedback')->where('id', $data['id'])->delete();

        if ($res) {
            return array('msg' => lang("deleted_successfully"), 'status' => 1);
        } else {
            return array('msg' => lang("failed_to_delete"), 'status' => 0);
        }
    }

    //反馈问题查看
    public function getfeedbackdetail($data)
    {
        $feedback_reply = getables('feedback_reply', ['feedback_id' => $data['id']], null, 'create_at asc');
        $feedback = array();
        foreach ($feedback_reply as $feedback_replykey => $feedback_replyval) {
            $feedback_reply[$feedback_replykey]['create_name'] = getValue('admin', ['id' => $feedback_replyval['create_id']], 'nickname');
            $feedback_reply[$feedback_replykey]['create_date'] = getTimeDate($feedback_replyval['create_at'], 12);
            $feedback_reply[$feedback_replykey]['time'] = time();
            $feedback_reply[$feedback_replykey]['uid'] = session('userid');
        }
        //追加当前的问题
        $resdata = getTree($feedback_reply, 0, 0, 'reply_id');
        $feeback = getinfo($data['id'], 'feedback');
        $feeback['create_name'] = getValue('admin', ['id' => $feeback['create_id']], 'nickname');
        $feeback['create_date'] = getTimeDate($feeback['create_at'], 12);
        $feeback['time'] = time();
        $feeback['uid'] = session('userid');
        return array('feedbackdetail' => $resdata, 'feedback' => $feeback);
    }

    //问题反馈提交的接口
    public function savedetail($data)
    {
        $ckdata = notEmpty(['content' => '反馈内容']);
        if ($ckdata['status'] == 2) {
            return $ckdata;
        }
        $update['create_id'] = $data['create_id'] = session('userid');
        $update['create_at'] = $data['create_at'] = time();
        $update['content'] = $data['content'];
        if (!$data['mark']) {
            $countone =getValue('feedback_reply',['feedback_id'=>$data['feedback_id']],'content');
            if(!$countone){
                //不用判断给谁回复的
                unset($data['id']);
                $res = Db::name('feedback_reply')->strict(false)->insert($data);
            }else{
                return array('msg' => '请选择要回复的问题', 'status' => 2);
            }
        } elseif ($data['mark'] == 'edit') {
            //回复表的编辑
            $res = Db::name('feedback_reply')->where('id', $data['id'])->update($update);
        }elseif ($data['mark'] == 'editfeekback') {
            //问题表的编辑
            $res = Db::name('feedback')->where('id', $data['id'])->update($update);
        } elseif ($data['mark'] == 'replyfeekback') {
            //针对问题进行的回复
            unset($data['id']);
            $res = Db::name('feedback_reply')->strict(false)->insert($data);
        } elseif ($data['mark'] == 'reply') {
            //回复表的回复
            $crete_id = getValue('feedback_reply', ['id' => $data['id']], 'create_id');
            if ($crete_id == session('userid')) {
                return array('msg' => '无法对自己进行回复', 'status' => 2);
            }
            $data['reply_id'] = $data['id'];
            unset($data['id']);
            $res = Db::name('feedback_reply')->strict(false)->insert($data);
        }
        //更新这个问题反馈的回复数量
        $count = Db::name('feedback_reply')->where('feedback_id', $data['feedback_id'])->count();
        Db::name('feedback')->where('id', $data['feedback_id'])->update(['replys' => $count]);
        if ($res || $res !== false) {
            return array('msg' => '编辑成功', 'status' => 1);
        } else {
            return array('msg' => '编辑失败', 'status' => 0);
        }
    }

    //问题的修复
    public function feedbackrepair($data)
    {
        $update['repair_id'] = session('userid');
        $update['repair_at'] = time();
        $updateres = $this->where('id', $data['id'])->update($update);
        if ($updateres !== false) {
            return array('msg' => '修复成功', 'status' => 1);
        } else {
            return array('msg' => '修复失败', 'status' => 0);
        }
    }
}